<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1100px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="140px">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>【项目】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目编号">
            <el-input v-model="model.projectcode" :disabled="true"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同金额">
            <el-input-number v-model="model.projectcontractmoney" controls-position="right" :step="1" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目金额">
            <el-input-number v-model="model.projectmoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收据总金额">
            <el-input-number v-model="model.projectreceiptmoney" controls-position="right" :step="1" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已收款金额">
            <el-input-number v-model="model.projectrecbillmoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>【业务类型】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="业务类型" prop="category" :rules="rules.validateRequire">
            <el-select v-model="model.category" filterable clearable @change="currentDatas" :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.categoryList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原收据号" prop="oldreceiptcode" v-if="isCancelAndReadd" :rules="isCancelAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.oldreceiptcode" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmReceiptApplyRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form class="tl-form" ref="categoryform" :model="model" :rules="rules" label-width="150px" v-show="model.category == '2' || model.category == '3'" :disabled="disableEditSubmit || disableSave"> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="作废收据回寄单号" prop="oldexpressno" v-if="isCancelAndReadd" :rules="rules.validateRequire">
            <el-input v-model="model.oldexpressno"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原客户名称" prop="oldcustomer" v-if="isCancelAndReadd">
            <el-input v-model="model.oldcustomer" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="原收据日期" prop="oldreceiptdate" v-if="isCancelAndReadd">
            <el-date-picker v-model="model.oldreceiptdate" type="date" placeholder="原开票日期" value-format="yyyy-MM-dd" disabled></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原收据金额" prop="oldreceiptmoney" v-if="isCancelAndReadd">
            <el-input-number v-model="model.oldreceiptmoney" controls-position="right" :step="1" :min="0" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- </el-form> -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="重开原因" prop="rereceiptreason" v-if="model.category == '3'" :rules="model.category == '3' ? rules.validateRequire : { required: false }">
            <el-input v-model="model.rereceiptreason" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="作废原因" prop="cancelreason" v-if="model.category == '2'" :rules="model.category == '2' ? rules.validateRequire : { required: false }">
            <el-input v-model="model.cancelreason" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form class="tl-form" ref="receiptform" :model="model" :rules="rules" label-width="150px" v-show="model.category == '1' || model.category == '3'" :disabled="disableEditSubmit || disableSave"> -->
      <el-row>
        <el-col :span="24" v-if="isAddAndReadd">
          <el-divider content-position="left"><span>【收据】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收据开具组织" prop="receiptorgname" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <tl-ref-org v-model="model.receiptorgname" :initDisplayData.sync="model.receiptorgname" dataType="code" :singleSelect="true" @confirm="onSelectOrg" :disabled="disableEditSubmit || disableSave"></tl-ref-org>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收据所属部门" prop="receiptdeptname" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <tl-ref-dept v-model="model.receiptdeptname" :initDisplayData.sync="model.receiptdeptname" dataType="code" nameType="fullname" :singleSelect="true" @confirm="onSelectDept" :disabled="disableEditSubmit || disableSave" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收据日期类型" prop="receiptdatetype" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.receiptdatetype" filterable clearable @change="currentReceiptDate" :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.receiptdatetypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收据日期" prop="receiptdate" v-if="model.receiptdatetype == '3' && (model.state == '9' || isAddAndReadd ) " :rules="model.receiptdatetype == '3' ? rules.validateRequire : { required: false }">
            <el-date-picker v-model="model.receiptdate" type="date" placeholder="收据日期" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收据金额" prop="receiptmoney" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input-number v-model="model.receiptmoney" controls-position="right" :step="1" :min="0" style="width:95%" :disabled="disableEditSubmit || disableSave"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="回款金额" prop="returnmoney" v-if="isAddAndReadd && model.state == '9'">
            <el-input-number v-model="model.returnmoney" controls-position="right" :step="1" :min="0" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="收据填写要求" prop="demand" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.demand" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="收据说明" prop="explain" v-if="isAddAndReadd">
            <el-input v-model="model.explain" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否邮寄" prop="ismail" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.ismail" filterable clearable @change="changeIsMail" :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.ismailList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="快递方式" prop="courier" v-if="isAddAndReadd&&model.ismail==='0'" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.courier" filterable clearable :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.courierList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customername" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.customername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业名称" prop="companyname" v-if="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.companyname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerBillRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="企业地址" prop="companyaddress" v-if="isAddAndReadd">
            <el-input v-model="model.companyaddress" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人性质" prop="receivertype" v-if="isAddAndReadd&&model.ismail==='0'" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.receivertype" filterable clearable :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.receivertypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收件人" prop="receiver" v-if="isAddAndReadd&&model.ismail==='0'" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.receiver" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人手机号" prop="receivertel" v-if="isAddAndReadd&&model.ismail==='0'">
            <el-input v-model="model.receivertel" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人固定电话" prop="receiverfixedtel" v-if="isAddAndReadd&&model.ismail==='0'">
            <el-input v-model="model.receiverfixedtel" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="邮寄地址" prop="mailaddress" v-if="isAddAndReadd&&model.ismail==='0'" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.mailaddress" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- </el-form> -->
      <el-form class="tl-form" ref="dealform" :model="model" :rules="rules" label-width="120px" v-if="model.state == '9' && model.receiptcode != null ">
        <el-row>
          <el-col :span="24">
            <el-divider content-position="left"><span>【收据邮寄】相关信息</span></el-divider>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收据号码" prop="receiptcode">
              <el-input v-model="model.receiptcode" :disabled="disModifyFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签收单是否返回" prop="isreceiptreturn">
              <el-select v-model="model.isreceiptreturn" filterable clearable :disabled="disModifyFlag">
                <el-option v-for="item in codeModel.isreceiptreturnList" :key="item.code" :label="item.name" :value="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="快递单号" prop="couriernumber">
              <el-input v-model="model.couriernumber" :disabled="disModifyFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="快递方式" prop="courier">
              <el-select v-model="model.courier" filterable clearable :disabled="disModifyFlag">
                <el-option v-for="item in codeModel.courierList" :key="item.code" :label="item.name" :value="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="实际收据日期" prop="actualreceiptdate">
              <el-date-picker v-model="model.actualreceiptdate" type="date" placeholder="实际收据日期" :disabled="disModifyFlag" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="寄出日期" prop="maildate">
              <el-date-picker v-model="model.maildate" type="date" placeholder="寄出日期" :disabled="disModifyFlag" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="orgname">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="username">
                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmReceiptApply" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
      <el-button type="primary" @click="doSubmitEditForModify" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disSaveForModify">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doModify" style="padding-left:2px" v-if="model.state===9 && model.modifyreceiptusercode.includes(user.code)">
        <svg-icon icon-class="el-icon-edit" />修改单据信息
      </el-button>
      <tl-attachment sourceType="PmReceiptApply" :sourceId="model.id" :disabled="disableAttachment" btnTitle="收据文件"></tl-attachment>
      <tl-attachment ref="attachmentRef" sourceType="PmReceiptApplySigned" :sourceId="model.id" btnTitle="收据签收单" :disabled="disableAttachment" :editable="false"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableDelApproval" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-receipt-apply-ref ref="tlpmReceiptApplyRef" @confirm="getpmReceiptApplyRefData" :isSingleSelect="true" :externalQueryParam="{qry_projectid:model.projectid,qry_isnotcancel:'0',qry_state:'9'}"></tlpm-receipt-apply-ref>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    <tlpm-customer-ref ref="pmCustomerRef" @confirm="getpmCustomerRefData" :isSingleSelect="true"></tlpm-customer-ref>
    <tlpm-customer-bill-ref ref="pmCustomerBillRef" @confirm="getpmCustomerBillRefData" :isSingleSelect="true" :externalQueryParam="{qry_customerid:model.customerid}"></tlpm-customer-bill-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmCustomerRef from '@/views/ProjectManage/pmCustomer/refList.vue'
import tlpmReceiptApplyRef from '@/views/ProjectManage/pmReceiptApply/refList.vue'
import tlpmCustomerBillRef from '@/views/ProjectManage/pmCustomerBill/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import Decimal from "decimal.js"
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmCustomerRef, tlpmCustomerBillRef, tlpmReceiptApplyRef },
  data() {
    return {
      moduleTitle: '项目收据申请',
      urlPrefix: '/pm/receiptApply/',
      disableAttachmentButton: true,
      url: {
        // 额外的url
      },
      disModifyFlag: true,
      disSaveForModify: true
    }
  },
  computed: {
    // 状态为新增或重开
    isAddAndReadd() {
      return this.model.category === '1' || this.model.category === '3'
    },
    // 状态为作废或重开
    isCancelAndReadd() {
      return this.model.category === '2' || this.model.category === '3'
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  created() {
    this.disModifyFlag = this.disableEditSubmit || this.disableSave
    this.disSaveForModify = this.disableSave
  },
  methods: {
    changeIsMail(e) {
      // 如果"是否邮寄"字段为是，则快递方式默认为顺丰
      if (e === '0') {
        this.$set(this.model, 'courier', '1')
      } else {
        this.$set(this.model, 'courier', '')
      }
    },
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //     this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        // this.$set(this.model, 'orgcode', this.org.code)
        // this.$set(this.model, 'orgname', this.org.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
        // this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        // 1 客户， 2 鼎信员工
        this.$set(this.model, 'receivertype', '1')
      }
    },
    // 排除各种容易造成垃圾数据的情况
    currentDatas() {
      if (this.model.category === '2') {
        // 清除垃圾数据
        this.$set(this.model, 'receiptorgname', '')
        this.$set(this.model, 'receiptorgcode', '')
        this.$set(this.model, 'receiptdeptname', '')
        this.$set(this.model, 'receiptdeptcode', '')
        this.$set(this.model, 'receiptdatetype', '')
        this.$set(this.model, 'receiptdate', '')
        this.$set(this.model, 'receiptmoney', '')
        this.$set(this.model, 'demand', '')
        this.$set(this.model, 'explain', '')
        this.$set(this.model, 'customername', '')
        this.$set(this.model, 'companyname', '')
        this.$set(this.model, 'companyaddress', '')
        this.$set(this.model, 'receiver', '')
        this.$set(this.model, 'receivertel', '')
        this.$set(this.model, 'mailaddress', '')
        this.$set(this.model, 'receivertype', '')
        this.$set(this.model, 'courier', '')
        this.$set(this.model, 'customerid', '')
        this.$set(this.model, 'rereceiptreason', '')
      }
      if (this.model.category === '1') {
        // 清除垃圾数据
        this.$set(this.model, 'oldid', '')
        this.$set(this.model, 'oldreceiptcode', '')
        this.$set(this.model, 'oldexpressno', '')
        this.$set(this.model, 'oldcustomer', '')
        this.$set(this.model, 'oldreceiptdate', '')
        this.$set(this.model, 'oldreceiptmoney', '')
        this.$set(this.model, 'cancelreason', '')
        this.$set(this.model, 'rereceiptreason', '')
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    loaded() {
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'PmReceiptApply',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
              this.$emit('refresh')
            }
          }
        })
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
      this.disSaveForModify = this.disableSave
      this.disModifyFlag = this.disableEditSubmit || this.disableSave
      this.disSaveForModify = this.disableSave
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    currentReceiptDate() {
      // this.$set(this.model, "receiptdate", '')
      // 1:开具当天 2：空着不填 3：指定日期
      if (this.model.receiptdatetype === '1') {
        this.$set(this.model, 'receiptdate', '')
      }
      if (this.model.receiptdatetype === '2') {
        this.$set(this.model, 'receiptdate', '')
      }
      // if (this.model.receiptdatetype === "3") {
      //     this.$set(this.model, "receiptdate", '')
      // }
    },
    onSelectDept(selectedDepts) {
      const selectedDept = selectedDepts[0]
      this.$set(this.model, 'receiptdeptname', selectedDept.fullname)
      this.$set(this.model, 'receiptdeptcode', selectedDept.code)
    },
    onSelectOrg(selectedOrgs) {
      const selectedOrg = selectedOrgs[0]
      this.$set(this.model, 'receiptorgname', selectedOrg.name)
      this.$set(this.model, 'receiptorgcode', selectedOrg.code)
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'projectid', selectionRow.id)
      this.$set(this.model, 'dispprojectid', selectionRow.name)
      this.$set(this.model, 'receiptorgname', selectionRow.ownerorgname)
      this.$set(this.model, 'receiptorgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'receiptdeptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'receiptdeptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'projectcode', selectionRow.code)
      this.$set(this.model, 'projectcontractmoney', selectionRow.contractmoney)
      this.$set(this.model, 'projectmoney', selectionRow.projectmoney)
      this.$set(this.model, 'projectrecbillmoney', selectionRow.recbillmoney)
      this.$set(this.model, 'projectreceiptmoney', selectionRow.receiptmoney)
      this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    },
    /** 打开项目收据参照 */
    openpmReceiptApplyRefDialog() {
      if (!this.model.projectid) {
        this.$message({
          message: '请先选择项目！',
          type: 'warning'
        })
        this.$refs.tlpmReceiptApplyRef.visible = false
      } else {
        this.$refs.tlpmReceiptApplyRef.visible = true
      }
    },
    /** 获取项目收据参数数据 */
    getpmReceiptApplyRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'oldid', selectionRow.id)
      this.$set(this.model, 'oldreceiptcode', selectionRow.receiptcode)
      // this.$set(this.model, 'oldexpressno', selectionRow.couriernumber)
      this.$set(this.model, 'oldcustomer', selectionRow.customername)
      this.$set(this.model, 'oldreceiptdate', selectionRow.receiptdate)
      this.$set(this.model, 'oldreceiptmoney', selectionRow.receiptmoney)
    },
    /** 打开客户名称参照 */
    openpmCustomerRefDialog() {
      this.$refs.pmCustomerRef.visible = true
    },
    /** 获取客户名称参数数据 */
    getpmCustomerRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'customerid', selectionRow.id)
      this.$set(this.model, 'customername', selectionRow.name)
      this.$set(this.model, 'companyname', '')
      this.$set(this.model, 'companyaddress', '')
      this.$set(this.model, 'receiver', '')
      this.$set(this.model, 'receivertel', '')
      this.$set(this.model, 'mailaddress', '')
    },
    /** 打开开票标识参照 */
    openpmCustomerBillRefDialog() {
      if (!this.model.customerid) {
        this.$message({
          message: '请先选择客户名称！',
          type: 'warning'
        })
        this.$refs.pmCustomerBillRef.visible = false
      } else {
        this.$refs.pmCustomerBillRef.visible = true
      }
    },
    // 修改收据信息
    doModify() {
      // 将发票号、是否快递、快递方式、快递单号字段置为可修改，且保存按钮可用
      this.disModifyFlag = false
      this.disSaveForModify = false
      this.modifyFlag = 1
      this.disableEditSubmit = false
      return
    },
    beforedoSubmitEditForModify() {
      if (this.model.ismail === '0') {
        if (this.model.receivertel === '' && this.model.receiverfixedtel === undefined) {
          this.$tap.alert('收件人手机号及收件人固定电话至少填写一个！', 'warning')
          return false
        }
        if (this.model.receivertel === null && this.model.receiverfixedtel === null) {
          this.$tap.alert('收件人手机号及收件人固定电话至少填写一个！', 'warning')
          return false
        }
        if (this.model.receivertel === '' && this.model.receiverfixedtel === '') {
          this.$tap.alert('收件人手机号及收件人固定电话至少填写一个！', 'warning')
          return false
        }
        // 校验手机号的合法性
        var telphone = /^1[3456789]\d{9}$/
        if (this.model.receivertel) {
          if (this.model.receivertel.length !== 11 || !telphone.test(this.model.receivertel)) {
            this.$tap.alert('收件人手机号格式不合法，请重新输入！', 'warning')
            return false
          }
        }
        // 校验固定电话
        var fixedtel = /^([0-9]{3,4}-)?[0-9]{7,8}$/
        if (this.model.receiverfixedtel) {
          if (!fixedtel.test(this.model.receiverfixedtel)) {
            this.$tap.alert('收件人固定电话格式不合法，请重新输入！', 'warning')
            return false
          }
        }
      }
      return true
    },
    doSubmitEditForModify() {
      if (!this.beforedoSubmitEditForModify()) {
        return
      }
      // 如果是修改个别字段
      if (this.modifyFlag === 1) {
        this.doSubmitModify()
        this.$nextTick(() => {
          this.visible = false
          this.modifyFlag = 0
        })
      } else {
        this.doSubmitEdit()
      }
    },
    /** 获取开票标识参数数据 */
    getpmCustomerBillRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'companyname', selectionRow.companyname)
      this.$set(this.model, 'companyaddress', selectionRow.companyaddress)
      this.$set(this.model, 'receiver', selectionRow.receiver)
      this.$set(this.model, 'receivertel', selectionRow.receivertel)
      this.$set(this.model, 'mailaddress', selectionRow.mailaddress)
    },
    // 修改特定字段
    doSubmitModify() {
      // 组装特定字段
      var updateParam = {}
      updateParam.id = this.model.id
      updateParam.receiptcode = this.model.receiptcode
      updateParam.isreceiptreturn = this.model.isreceiptreturn
      updateParam.couriernumber = this.model.couriernumber
      updateParam.courier = this.model.courier
      updateParam.actualreceiptdate = this.model.actualreceiptdate
      updateParam.maildate = this.model.maildate
      postAction("/pm/receiptApply/modifyDoc", updateParam).then((res) => {
        this.$tap.alert(res.message)
      })
    },
    beforeApproval() {
      // 如果"是否邮寄"字段为否的话，则清空收件人性质、收件人、收件人手机号、收件人固定电话、邮
      // 寄地址字段
      if (this.model.ismail === '1') {
        this.$set(this.model, 'receivertype', '')
        this.$set(this.model, 'receiver', '')
        this.$set(this.model, 'receivertel', '')
        this.$set(this.model, 'receiverfixedtel', '')
        this.$set(this.model, 'mailaddress', '')
        this.$set(this.model, 'courier', '')
      }
      return true
    },
    /** 提交前执行 */
    beforeSubmit() {
      if (this.model.category === '2') {
        // 清除垃圾数据
        this.$set(this.model, 'receiptorgname', '')
        // 作废的审批流程需要用到receiptorgcode,故不清理
        // this.$set(this.model, 'receiptorgcode', '')
        this.$set(this.model, 'receiptdeptname', '')
        this.$set(this.model, 'receiptdeptcode', '')
        this.$set(this.model, 'receiptdatetype', '')
        this.$set(this.model, 'receiptdate', '')
        this.$set(this.model, 'receiptmoney', '')
        this.$set(this.model, 'demand', '')
        this.$set(this.model, 'explain', '')
        this.$set(this.model, 'customername', '')
        this.$set(this.model, 'companyname', '')
        this.$set(this.model, 'companyaddress', '')
        this.$set(this.model, 'receiver', '')
        this.$set(this.model, 'receivertel', '')
        this.$set(this.model, 'mailaddress', '')
        this.$set(this.model, 'receivertype', '')
        this.$set(this.model, 'courier', '')
        this.$set(this.model, 'customerid', '')
        this.$set(this.model, 'rereceiptreason', '')
      }
      if (this.model.category === '1') {
        // 清除垃圾数据
        this.$set(this.model, 'oldid', '')
        this.$set(this.model, 'oldreceiptcode', '')
        this.$set(this.model, 'oldexpressno', '')
        this.$set(this.model, 'oldcustomer', '')
        this.$set(this.model, 'oldreceiptdate', '')
        this.$set(this.model, 'oldreceiptmoney', '')
        this.$set(this.model, 'cancelreason', '')
        this.$set(this.model, 'rereceiptreason', '')
      }
      // 申请收据金额 合同总金额 已开票金额
      const { receiptmoney, projectcontractmoney, projectreceiptmoney, oldreceiptmoney } = this.model
      if (
        (receiptmoney || receiptmoney === 0) &&
        (projectcontractmoney || projectcontractmoney === 0) &&
        (projectreceiptmoney || projectreceiptmoney === 0)
      ) {
        // 恢复因为js浮点计算缺失的精度
        // const res = Decimal((this.model.receiptmoney === '' || this.model.receiptmoney === undefined || this.model.receiptmoney === null) ? 0 : this.model.receiptmoney).add(Decimal((this.model.projectreceiptmoney === '' || this.model.projectreceiptmoney === undefined || this.model.projectreceiptmoney === null) ? 0 : this.model.projectreceiptmoney)).sub(Decimal((this.model.oldreceiptmoney === '' || this.model.oldreceiptmoney === undefined || this.model.oldreceiptmoney === null) ? 0 : this.model.oldreceiptmoney)).sub(Decimal((this.model.projectcontractmoney === '' || this.model.projectcontractmoney === undefined || this.model.projectcontractmoney === null) ? 0 : this.model.projectcontractmoney))
        const res = Decimal(this.checkData(this.model.receiptmoney) ? 0 : this.model.receiptmoney).add(Decimal(this.checkData(this.model.projectreceiptmoney) ? 0 : this.model.projectreceiptmoney)).sub(Decimal(this.checkData(this.model.oldreceiptmoney) ? 0 : this.model.oldreceiptmoney)).sub(Decimal(this.checkData(this.model.projectcontractmoney) ? 0 : this.model.projectcontractmoney))
        // if (receiptmoney + projectreceiptmoney - oldreceiptmoney > projectcontractmoney) {
        //   this.$tap.alert('收据金额与项目已收据金额之和不能高于合同总金额！', 'warning')
        //   return false
        // }
        if (res > 0) {
          this.$tap.alert('收据金额与项目已收据金额之和不能高于合同总金额！', 'warning')
          return false
        }
      }
      return true
    },
    // 检查当前属性是否有数据
    checkData(item) {
      return (item === '' || item === undefined || item === null)
    }
  }
}
</script>

